package com.nebula.service.impl;

import com.nebula.entity.Role;
import com.nebula.common.PageResult;
import com.nebula.dao.IRoleDao;
import com.nebula.dao.IRoleNavigationDao;
import com.nebula.dao.impl.RoleDaoImpl;
import com.nebula.dao.impl.RoleNavigationDaoImpl;
import com.nebula.service.IRoleService;
import com.nebula.util.DateTimeUtil;
import com.nebula.vo.RoleVo;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author CcccX
 * @create 2019-01-15 14:32
 */
public class RoleServiceImpl implements IRoleService {
    private final IRoleDao iRoleDao = new RoleDaoImpl();
    private final IRoleNavigationDao iRoleNavigationDao = new RoleNavigationDaoImpl();

    @Override
    public List<RoleVo> findAll() {
        List<Role> roles = iRoleDao.findAll();
        List<RoleVo> roleVos = new ArrayList<>();
        for (Role role : roles) {
            RoleVo roleVo = assembleRoleVo(role);
            roleVos.add(roleVo);
        }

        return roleVos;
    }

    @Override
    public Map<String, Object> selectRoleIdByNavIdFromRoleNavigation(String navId) {
        return iRoleDao.selectRoleIdByNavIdFromRoleNavigation(navId);
    }

    @Override
    public Role findById(int roleId) {
        return iRoleDao.findById(roleId);
    }

    @Override
    public PageResult<RoleVo> findByPage(int page, int size, int roleId) {
        //封装查询条件
        String conditions = "";
        Role roleLevel = findById(roleId);
        if (roleLevel.getRoleLevel() == 0) {
            conditions = " where role_level >= 0";
        } else {
            conditions = " where role_level > " + roleLevel.getRoleLevel();
        }

        List<Role> roles = iRoleDao.findByPage(page, size, conditions);
        Integer totalCount = iRoleDao.selectCount("role", conditions);
        List<RoleVo> roleVos = new ArrayList<>();
        for (Role role : roles) {
            RoleVo roleVo = assembleRoleVo(role);
            roleVos.add(roleVo);
        }
        PageResult<RoleVo> pageResult = new PageResult<>();
        pageResult.setCurrPage(page);
        pageResult.setPageSize(size);
        pageResult.setTotalCount(totalCount);

        Double dc = Math.ceil(totalCount.doubleValue() / size);
        pageResult.setTotalCount(dc.intValue());
        pageResult.setList(roleVos);

        return pageResult;
    }

    @Override
    public void save(Role role) {
        iRoleDao.save(role);
    }

    @Override
    public int delete(int id) {
        //删除关系表role_navigation数据
        iRoleNavigationDao.deleteByRoleId(id);

        return iRoleDao.delete(id);
    }

    @Override
    public void update(Role role) {
        iRoleDao.update(role);
    }

    private RoleVo assembleRoleVo(Role role) {
        RoleVo roleVo = new RoleVo();

        roleVo.setId(role.getId());
        roleVo.setRole(role.getRole());
        roleVo.setUser(role.getUser());
        roleVo.setUserRole(role.getUserRole());
        roleVo.setInstruction(role.getInstruction());
        roleVo.setCreatedTime(DateTimeUtil.dateToStr(role.getCreatedTime()));
        roleVo.setUpdatedTime(DateTimeUtil.dateToStr(role.getUpdatedTime()));

        return roleVo;
    }
}
